// pages/selfAssessment/question/index.js
const api = require('.././../../config/api')
const request = require('.././../../utils/request')
let app = getApp()
let handleDo = false
import lottie from '.././../../utils/lottie'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    questionnaireInfo: {}, // 问卷说明
    key: '',
    pathId: '',
    taskFlowId: '',
    fromOrigin: 0,// 从活动训练还是普通训练进入
    activityId: '',// 从活动训练训练进入接收活动id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init()
    this.getQuestionnaireDetails(options.id)
    let key = ''
    let pathId = ''
    let taskFlowId = ''
    if (options.key) {
      key = 'training'
    }
    if (options.pathId) {
      pathId = options.pathId
    }
    if (options.taskFlowId) {
      taskFlowId = options.taskFlowId
    }

    this.setData({
      key: key,
      pathId: pathId,
      taskFlowId: taskFlowId,
      fromOrigin: Number(options?.fromOrigin) || 0,
      activityId: options?.activityId || '',
    })
  },
  // 初始化加载动画
  init() {
    if (this.inited) {
      return
    }
    wx.createSelectorQuery()
      .selectAll('#begin_question')
      .node((res) => {
        const canvas = res[0].node
        const context = canvas.getContext('2d')
        canvas.width = 300
        canvas.height = 300
        lottie.setup(canvas)
        this.ani = lottie.loadAnimation({
          loop: true,
          autoplay: true,
          animationData: require('./data.js'),
          rendererSettings: {
            context,
          },
        })
        this.inited = true
      })
      .exec()
  },
  // 获取问卷说明
  getQuestionnaireDetails: async function (id) {
    const res = await request.get(
      api.getQuestionnaireDetails,
      { questionnaireId: id },
      { loading: false }
    )
    if (res.httpStatus === 200) {
      this.setData({
        questionnaireInfo: res.data,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const patientInfo = wx.getStorageSync('patientInfo')
    if (patientInfo) {
      this.setData({
        patientInfo: patientInfo,
      })
    } else {
      this.setData({
        patientInfo: '',
      })
    }
    handleDo = false
  },
  begin() {
    handleDo = true
    let { key, pathId, taskFlowId, questionnaireInfo, fromOrigin, activityId } = this.data
    wx.redirectTo({
      url: `/pages/selfAssessment/question/subject/subject?questionnaireId=${questionnaireInfo.id}&questionType=${questionnaireInfo.questionType}&key=${key}&pathId=${pathId}&taskFlowId=${taskFlowId}&pinyinCode=${questionnaireInfo.pinyinCode}&questionKey=${questionnaireInfo?.questionKey}&fromOrigin=${fromOrigin}&activityId=${activityId}`,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.globalData.createPatientEntrance = ''
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (handleDo) {
      return
    }
    if (app.globalData.isFirstTask) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
    app.globalData.isFirstTask = false
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
